﻿@model Nutshell.Blog.ViewModel.Article.DetailModel
@{
    Layout = "~/Views/Shared/_BlogHomeLayout.cshtml";
    ViewBag.Title = $"{Model.Article.Title} - {Model.Article.Author?.Nickname} - 果壳园";
}

@section style{
    <link href="~/Content/style/detail-common.css" rel="stylesheet" />
}
<input id="article" type="hidden" value="@Model.Article.Article_Id" />
<div id="post_detail">
    <div id="topics">
        <div class="post">
            <h1 class="postTitle">
                <a href="#">@Model.Article.Title</a>
            </h1>
            <div class="postBody">
                <div id="post_body">
                    @Html.Raw(HttpUtility.HtmlDecode(Model.Article.Body))
                </div>
                <div id="blog_post_info_block">
                    <div id="BlogPostCategory">
                        分类：
                        <a href="#">
                            @(Model.Article.CustomCategory?.CategoryName)
                        </a>
                    </div>
                    <div id="blog_post_info">
                        <div id="green_channel">
                            <a href="javascript:void(0);" id="green_channel_follow">关注我</a>
                            @{
                                var hava = (bool)(ViewBag.HavaCollection ?? false);
                                // 已收藏
                                if (hava)
                                {
                            <span>已收藏</span>
                                }
                                else
                                {
                            <a href="javascript:void(0);" id="green_channel_favorite">收藏该文</a>
                                }
                            }
                        </div>
                    </div>
                    <div id="author_profile">
                        <div id="author_profile_info" class="author_profile_info">
                            <a href="#">
                                <img src="/upload/photos/default.png" alt="加载失败" class="author_avatar" />
                            </a>
                            <div id="" class="author_profile_info">
                                <a href="/@Model.Article.Author.Login_Name">@Model.Article.Author.Nickname</a>
                                <br />
                                <a href="#">关注</a>
                                <br />
                                <a href="#">粉丝</a>
                            </div>
                        </div>
                        <div id="author_profile_follow">
                            <a href="#">+加关注</a>
                        </div>
                    </div>
                    <div id="post_next_prev">
                        @{
                            var before = ViewBag.Before as Nutshell.Blog.Model.Article;
                            var next = ViewBag.Next as Nutshell.Blog.Model.Article;
                            if (before != null)
                            {
                        <a href="/@before.Author.Login_Name/p/@(before.Article_Id).html" class="fa fa-angle-double-left">上一篇： @before.Title</a>
                        <br />
                            }
                            if (next != null)
                            {
                        <a href="/@next.Author.Login_Name/p/@(next.Article_Id).html" class="fa fa-angle-double-right">下一篇： @next.Title</a>
                            }
                        }
                    </div>
                </div>
            </div>
            <div class="postDesc">
                posted @@ <span class="post-date">@Model.Article.Creation_Time</span> @Model.Article.Author.Nickname 阅读(0) 评论(0) <a href="#">编辑</a> <a href="#">收藏</a>
            </div>
        </div>
    </div>

    <div id="blog-comments-placeholder">
        @*@if (Model.Discussions.Count > 0)
            {
                <div class="feedback_area_title">
                    评论列表
                </div>
                var i = 1;
                var Discussions = Model.Discussions.OrderBy(d => d.Discussion_Time);
                foreach (var item in Discussions)
                {
                    <div class="feedbackItem">
                        <div class="feedbackListSubtitle">
                            <div class="feedbackManage">
                                <span class="comment_actions">
                                    <a href="#">回复</a>
                                </span>
                            </div>
                            <a href="#" class="layer">#@i</a>
                            @if (item.User_Id == item.Article.Author_Id)
                            {
                                @("[")<span>楼主</span>@("]")
                            }
                            <span class="comment_date">@item.Discussion_Time</span>
                            <a href="/@item.User.Login_Name">@item.User.Nickname</a>
                        </div>
                        <div class="feedbackCon">
                            <div class="blog_comment_body">
                                @if (item.ParentDiscussion != null)
                                {
                                    <a href="/">@@</a>
                                    @item.ParentDiscussion.User.Nickname
                                    <br />
                                }
                                @Html.Raw(HttpUtility.HtmlDecode(item.Content))
                            </div>
                        </div>
                    </div>
                    i++;
                }
            }*@
    </div>
    <div id="comment_form">
        <div id="comment_nav">
            <a href="javascript:void();" onclick="getcomments();">刷新评论</a>
            <a href="javascript:void();" onclick="location.reload(true);">刷新页面</a>
            <a href="javascript:void();" data-am-smooth-scroll>返回顶部</a>
        </div>
        <div id="comment_form_container">
            <div id="commentform_title">发表评论</div>
            <div id="commentform_content">
                <textarea style="width:100%" rows="5" id="content"></textarea>
                <input type="button" id="btnSubmit" class="am-btn am-btn-secondary" value="提交评论" data-am-loading="{loadingText: '努力加载中...'}" />
            </div>
        </div>
    </div>
</div>

@section categories{
    @Html.Partial("Article/_Categories", Model.CustomCategories, new ViewDataDictionary() {
        new KeyValuePair<string, object>("UserName", Model.Article.Author.Login_Name)
    })
}

@section archives{
    @Html.Partial("Article/_Archives", Model.Archives)
}

@section script{
    <script>
        $(function () {
            var id = $("#article").val();
            $("#btnSubmit").click(function () {
                var content = $("#content").val().trim();
                console.log(content);
                if (content.length <= 0) {
                    layer.msg("评论内容不能为空！", { time: 1000, icon: 5 });
                    return;
                }
                $.post("/article/comment", { content: content, id: id }, function (data) {
                    console.log(data);
                    if (data.code == 0) {
                        layer.msg(data.msg, { time: 2000, icon: 6 });
                        $("#content").val('');
                        getcomments();
                    } else {
                        layer.msg(data.msg, { time: 2000, icon: 5 });
                    }
                })
            });

            $("#green_channel_favorite").click(function () {
                layer.open({
                    type: 2,
                    title: '添加收藏',
                    area: ['450px', '400px'],
                    fixed: false, //不固定
                    content: '/favorite/add?articleId=' + id
                });
            });
            getcomments();
        })

        function getcomments() {
            var $place = $("#blog-comments-placeholder");
            $place.empty();
            var id = $("#article").val();
            $.get("/article/getcomments", { id: id }, function (data) {
                console.log(data);
                if (data.code == 0) {
                    var res = data.res;
                    var count = res.length;
                    if (count > 0) {
                        $place.append('<div class="feedback_area_title">评论列表</div>');
                        for (var i = 0; i < count; i++) {
                            res[i]["Discussion_Time"].replace(/Date\([\d+]+\)/, function (a) { eval('d = new ' + a) });
                            var html = '';
                            html += ('<div class="feedbackItem"><div class="feedbackListSubtitle"><div class="feedbackManage"><span class="comment_actions"><a href="#">回复</a></span></div><a href="#" class="layer">#' + (i + 1) + '&nbsp;</a><span class="comment_date">' + d.format("yyyy-MM-dd hh:mm:ss") + '&nbsp;</span><a href="/' + res[i]["userName"] + '">' + res[i]["Nickname"] + '&nbsp;</a></div> <div class="feedbackCon"><div class="blog_comment_body">');
                            if (res[i]["Reply_Id"] != null) {
                                html += ('@@' + res[i]["parentNick"] + '</br>');
                            }
                            html += (res[i]["Content"] + '</div></div></div>');
                            $place.append(html);
                        }
                    }
                }
            })
        }
    </script>
}
